/*
 * @Description: 用户行为数据收集
 * @Author: LiuZhen
 * @Date: 2019-04-28 15:51:06
 * @Last Modified by: LiuZhen
 * @Last Modified time: 2019-06-17 17:03:30
 */
import BaseInfoCollection from './BaseInfoCollection';

export default class BehaviorInfoCollection extends BaseInfoCollection {
  constructor(options) {
    super(options);
    this._baseInfoObject = null;
    this._staticInfoObject = (baseInfoObject) => {
      return {
        baseInfo: baseInfoObject,
        reportType: `behavior_info`,
        distinctId: baseInfoObject.distinctId,
        businessId: baseInfoObject.businessId,
      }
    }
  }

  // 用户登入系统事件
  async userLoginEvent(userId) {
    if (!userId) {
      throw new Error(`Need to pass the parameter uid`);
    }

    if (!this._baseInfoObject) {
      this._baseInfoObject = await this._getBaseInfoObject();
    }

    this.pushInfoData({
      userId,
      eventId: 'CH003-houseinfor-detailview'
    }, 'behavior_info');
  }

  // 用户登出系统事件
  userLogoutEvent() {
    // ...
  }

  // 页面浏览事件
  async pageViewEvent(obj) {
    const objParams = obj ? obj : {};

    if (!this._baseInfoObject) {
      this._baseInfoObject = await this._getBaseInfoObject();
    }

    const resultObj = Object.assign(
      objParams, 
      this._staticInfoObject(this._baseInfoObject),
      { eventType: `pageview` }
    );

    this.pushInfoData(resultObj, 'behavior_info');
  }

  // 详情浏览事件
  async detailViewEvent(obj) {
    const objParams = obj ? obj : {};

    if (!this._baseInfoObject) {
      this._baseInfoObject = await this._getBaseInfoObject();
    }

    const resultObj = Object.assign(
      objParams, 
      this._staticInfoObject(this._baseInfoObject),
      { eventType: `detailview` }
    );

    this.pushInfoData(resultObj, 'behavior_info');
  }

  // 菜单跳转事件
  menuSwitchEvent() {
    // ...
  }

  // 点击事件
  async clickEvent(obj) {
    const objParams = obj ? obj : {};

    if (!this._baseInfoObject) {
      this._baseInfoObject = await this._getBaseInfoObject();
    }

    const resultObj = Object.assign(
      objParams, 
      this._staticInfoObject(this._baseInfoObject),
      { eventType: `click` }
    );

    this.pushInfoData(resultObj, 'behavior_info');
  }

  // 搜索事件
  async searchEvent(obj) {
    const objParams = obj ? obj : {};

    if (!this._baseInfoObject) {
      this._baseInfoObject = await this._getBaseInfoObject();
    }

    const resultObj = Object.assign(
      objParams, 
      this._staticInfoObject(this._baseInfoObject),
      { eventType: `search` }
    );

    this.pushInfoData(resultObj, 'behavior_info');
  }

  // 广告转换
  adTouchEvent() {
    // ...
  }

  // 查询列表事件
  listSearchEvent() {
    // ...
  }

  // 新建条目提交事件
  newItemSubmitEvent() {
    // ...
  }

  // 修改条目提交事件
  updateItemSubmitEvent() {
    // ...
  }

  // 删除条目提交事件
  deleteItemSubmitEvent() {
    // ...
  }

  // 上传（导入）事件
  uploadInfoEvent() {
    // ...
  }

  // 下载（导出）事件
  downloadInfoEvent() {
    // ...
  }

  /** 
   *  @description 统一事件处理
   *  @param obj 额外的参数对象
   *  @param eventName 事件名称
   *  @returns void 
   */
  async handleEventReport(obj, eventName = '') {
    const objParams = obj ? obj : {};

    if (!this._baseInfoObject) {
      this._baseInfoObject = await this._getBaseInfoObject();
    }

    const resultObj = Object.assign(
      objParams, 
      this._staticInfoObject(this._baseInfoObject),
      { eventType: eventName }
    );

    this.pushInfoData(resultObj, 'behavior_info');
  }

  // 私有方法，调用基类方法 dataInfo 获取基础信息
  async _getBaseInfoObject() {
    const data = await this.dataInfo();
    return data;
  }
}